<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <el-button type="primary" @click="getTwoLevel">添加分类</el-button>
      <el-table :data="cateList" border stripe row-key="cat_id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <!-- <el-table-column type="index"> </el-table-column> -->
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cat_deleted" label="是否有效">
          <template slot-scope="scope">
            <i v-if="scope.row.cat_deleted == true" class="el-icon-success" style="color: red"></i>

            <i v-if="scope.row.cat_deleted == false" class="el-icon-success" style="color: #1b1"></i>
          </template>
        </el-table-column>
        <el-table-column prop="cat_level" label="排序">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
            <el-tag v-if="scope.row.cat_level == 1">二级</el-tag>
            <el-tag v-if="scope.row.cat_level == 2">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="role_name" label="操作">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-edit" @click="handleEdit(scope.row)">删除</el-button>
        </el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>

    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="50%" @close="closeAddDialog">
      <el-form :model="addCateForm" :rules="addCateRules" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader v-model="selectKeys" :options="parentCateList" :props="{ expandTrigger: 'hover', value: 'cat_id', label: 'cat_name', children: 'children', checkStrictly: 'true' }" @change="handleChange" clearable></el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="closeAddDialog">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  data() {
    return {
      cateList: [],
      // 查询条件
      queryInfo: {
        type: 3,
        // 当前页码数
        pagenum: 1,
        // 每页显示多少条数据
        pagesize: 5
      },
      total: 0,
      addDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0, //分类父ID
        cat_level: 0
      },
      addCateRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      parentCateList: {},
      selectKeys: [],
      tableData: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取所有商品
    async getCateList() {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.cateList = res.data.result
      this.total = res.data.total
      console.log(this.cateList)
    },
    // 分页部分
    // 一页显示几条
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getCateList()
    },
    // 当前显示的是第几页
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getCateList()
    },
    async getTwoLevel() {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })

      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.parentCateList = res.data
      this.addDialogVisible = true
      console.log('aaa', this.parentCateList)
    },
    handleChange() {
      console.log(this.selectKeys)
      if (this.selectKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
        this.addCateForm.cat_level = this.selectKeys.length
      }
    },
    closeAddDialog() {
      this.$refs.addCateFormRef.resetFields()
      this.addDialogVisible = false
      this.selectKeys = []
    },
    addCate() {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.closeAddDialog()
        this.getCateList()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
